package com.hundsun.cprs.yyt.service.dilivery.service.impl;

import com.hundsun.cprs.yyt.common.base.BaseService;
import com.hundsun.cprs.yyt.common.enums.EnumBusinessCode;
import com.hundsun.cprs.yyt.common.exception.ServiceCodeException;
import com.hundsun.cprs.yyt.service.dilivery.dao.DiliveryAddressDao;
import com.hundsun.cprs.yyt.service.dilivery.domain.DiliveryAddress;
import com.hundsun.cprs.yyt.service.dilivery.domain.query.AddressQuery;
import com.hundsun.cprs.yyt.service.dilivery.enums.EnumDiliveryAddress;
import com.hundsun.cprs.yyt.service.dilivery.service.DiliveryAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class DiliveryAddressServiceImpl extends BaseService implements DiliveryAddressService {
	@Autowired
	private DiliveryAddressDao diliveryAddressdao;
	
	@Override
	@Transactional
	public void createDiliveryAddress(DiliveryAddress diliveryAddress) throws ServiceCodeException {
		try{
			//参数校验
			validateParam(diliveryAddress);
			//新增提货地址先校验是否是个人的第一个地址，如果是第一个则置为默认
			List<DiliveryAddress> diliveryAddressList=diliveryAddressdao.selectAddressListByUserId(diliveryAddress.getUserId());
			if(diliveryAddressList.isEmpty()){
				diliveryAddress.setIsDefault(EnumDiliveryAddress.Y_DEFAULT_ADDRESS.getCode());
			}else{
				//如果新增的不是个人的第一个地址，那么校验新增的地址是否设置为了默认地址
				if(diliveryAddress.getIsDefault()!=null&&diliveryAddress.getIsDefault()!=""){
					if(diliveryAddress.getIsDefault().equals(EnumDiliveryAddress.Y_DEFAULT_ADDRESS.getCode())){
						//如果新增的地址设置为默认地址，那么需要将原默认地址置为非默认
						diliveryAddressdao.setNoDefaultByUserId(diliveryAddress.getUserId());
					}
				}else{
					diliveryAddress.setIsDefault(EnumDiliveryAddress.N_DEFAULT_ADDRESS.getCode());
				}
			}
			diliveryAddress.setIsDelete(EnumDiliveryAddress.N_DELETE_DISABLE.getCode());
			diliveryAddressdao.createDiliveryAddress(diliveryAddress);
		}catch(ServiceCodeException e){
			logger.error("用户："+diliveryAddress.getUserId()+"新增提货地址失败,原因："+e.getErrorDesc());
			throw e;
		}
	}

	@Override
	@Transactional
	public void modifiyDiliveryAddress(DiliveryAddress diliveryAddress) throws ServiceCodeException {
		try{
			//必要参数校验
			if(diliveryAddress.getId()==null){
				throw new ServiceCodeException(EnumBusinessCode.BUSINESS_1000.getErrorNo(),EnumBusinessCode.BUSINESS_1000.getErrorInfo());
			}
			if(diliveryAddress.getUserId()==null){
				throw new ServiceCodeException(EnumBusinessCode.BUSINESS_1000.getErrorNo(),EnumBusinessCode.BUSINESS_1000.getErrorInfo());
			}
			//如果当前需要修改的地址设置为了默认地址
			if(diliveryAddress.getIsDefault()!=null){
				if(diliveryAddress.getIsDefault().equals(EnumDiliveryAddress.Y_DEFAULT_ADDRESS.getCode())){
					//先将用户的所有地址设置为非默认
					diliveryAddressdao.setNoDefaultByUserId(diliveryAddress.getUserId());
				}
			}
			diliveryAddressdao.modifiyDiliveryAddress(diliveryAddress);
		}catch(ServiceCodeException e){
			logger.error("用户："+diliveryAddress.getUserId()+"修改提货地址失败,原因："+e.getErrorDesc());
			throw e;
		}

	}

	@Override
	public void deleteDiliveryAddress(Long userId,Long addressId) throws ServiceCodeException {
		try{
			DiliveryAddress diliveryAddress = new DiliveryAddress();
			diliveryAddress = diliveryAddressdao.selectAddressByAddressId(addressId);
			if (diliveryAddress==null){
				throw new ServiceCodeException("此用户未绑定收货地址，删除失败。");
			}
			diliveryAddress.setUserId(userId);
			diliveryAddress.setId(addressId);
			diliveryAddress.setIsDelete(EnumDiliveryAddress.Y_DELETE_ABLE.getCode());
			diliveryAddressdao.modifiyDiliveryAddress(diliveryAddress);
		}catch(ServiceCodeException e){
			logger.error("用户："+addressId+"删除提货地址失败,原因："+e.getErrorDesc());
			throw e;
		}

	}

	@Override
	@Transactional
	public void alterDiliveryAddressToDefault(Long userId, Long addressId) throws ServiceCodeException {
		try{
			//先将用户的所有地址设置为非默认
			diliveryAddressdao.setNoDefaultByUserId(userId);
			//再将本地址设置为默认
			DiliveryAddress diliveryAddress = new DiliveryAddress();
			diliveryAddress.setId(addressId);
			diliveryAddress.setUserId(userId);
			diliveryAddress.setIsDefault(EnumDiliveryAddress.Y_DEFAULT_ADDRESS.getCode());
			diliveryAddressdao.modifiyDiliveryAddress(diliveryAddress);
		}catch(ServiceCodeException e){
			logger.error("用户："+addressId+"设置提货地址为默认失败,原因："+e.getErrorDesc());
			throw e;
		}

	}

	@Override
	public List<DiliveryAddress> selectAddressListByUserId(Long userId) {
		return diliveryAddressdao.selectAddressListByUserId(userId);
	}
	
	public void validateParam(DiliveryAddress diliveryAddress){
		if(diliveryAddress.getUserId()==null){
			throw new ServiceCodeException(EnumBusinessCode.BUSINESS_1000.getErrorNo(),EnumBusinessCode.BUSINESS_1000.getErrorInfo());
		}
		if(diliveryAddress.getProvinceAddress()==null || diliveryAddress.getProvinceAddress().trim()==""){
			throw new ServiceCodeException(EnumBusinessCode.BUSINESS_1000.getErrorNo(),EnumBusinessCode.BUSINESS_1000.getErrorInfo());
		}
		if(diliveryAddress.getMunicipalAddress()==null || diliveryAddress.getMunicipalAddress().trim()==""){
			throw new ServiceCodeException(EnumBusinessCode.BUSINESS_1000.getErrorNo(),EnumBusinessCode.BUSINESS_1000.getErrorInfo());
		}
		if(diliveryAddress.getCountyAddress()==null || diliveryAddress.getCountyAddress().trim()==""){
			throw new ServiceCodeException(EnumBusinessCode.BUSINESS_1000.getErrorNo(),EnumBusinessCode.BUSINESS_1000.getErrorInfo());
		}
		if(diliveryAddress.getDiliveryAddress()==null || diliveryAddress.getDiliveryAddress().trim()==""){
			throw new ServiceCodeException(EnumBusinessCode.BUSINESS_1000.getErrorNo(),EnumBusinessCode.BUSINESS_1000.getErrorInfo());
		}
	}

	@Override
	public DiliveryAddress selectDefaultAddressByUserId(Long userId) {
		return diliveryAddressdao.selectAddressByUserIdOrAddressId(userId, null);
	}

	@Override
	public DiliveryAddress selectAddressDetailByAddressId(Long addressId) {
		return diliveryAddressdao.selectAddressByUserIdOrAddressId(null, addressId);
	}

	@Override
	public void queryAddressByPage(AddressQuery query) {
		List<DiliveryAddress> diliveryAddresses = diliveryAddressdao.queryAddressByPage(query);
		query.compatible(diliveryAddresses);
	}
}
